package com.juyu.miliao.api.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.juyu.miliao.api.dao.mysql.UserPhotoDao;
import com.juyu.miliao.api.domain.UserPhoto;

@Service
public class UserPhotoService {
	
	@Resource
	private UserPhotoDao userPhotoDao;

	public List<UserPhoto> getUserPhotoById(long id) {
		return userPhotoDao.getUserPhotoById(id);
	}

	@Transactional(rollbackFor = Exception.class)
	public void updateUserPhoto(UserPhoto entity) {
		userPhotoDao.deleteUserPhotoById(entity.getUserId());
		if(entity.getPicUrl()!=null && !entity.getPicUrl().equals("") &&!entity.getPicUrl().equals("null")){
			UserPhoto temp = new UserPhoto();
			temp.setUserId(entity.getUserId());
			if(entity.getPicUrl().contains(",")){
				String [] array = entity.getPicUrl().split(",");
				for(int i = 0;i<array.length;i++){
					temp.setPicUrl(array[i]);
					userPhotoDao.insertUserPhoto(temp);
				}
			}else{
				userPhotoDao.insertUserPhoto(entity);
			}
		}
	}
	
}
